package com.lzh.template.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.lzh.template.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 插入数据自动填充字段
 * 在需要填充的字段上添加@TableField(fill = ...)
 *
 * @author luzhiheng
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill...");
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("createName", SecurityUtils.getUsername(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill...");
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateName", SecurityUtils.getUsername(), metaObject);
    }
}

